﻿#ifndef MYGUI_H
#define MYGUI_H

#include <QtCore>
#include <QColor>

#include <QLineEdit>
#include <QSpinBox>
#include <QComboBox>
#include <QGroupBox>

#include <QTableWidget>
#include <QHeaderView>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QTableWidgetItem>

#include <QApplication>
#include <QDesktopWidget>
#include <QFontDatabase>
#include <QGraphicsDropShadowEffect>

#include "Window/Common/messagebox.h"
#include "Window/Common/messagetip.h"

class MyGUI: public QObject
{

public:

    //设置字体
    static void SetFont(){
        int fontId = QFontDatabase::addApplicationFont(":/fonts/larry-icon.ttf");//从文件名制定的文件加载字体
        QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);//获得fintid给定的字体列表
        QFont font;
        font.setPointSize(9);
        //font.setLetterSpacing(QFont::AbsoluteSpacing,1.2);
        font.setFamily("larry-icon ,Microsoft YaHei");//设置字体族名称
        qApp->setFont(font);
    }


    //设置皮肤样式
    static void SetStyle(const QString &styleName)
    {
        QFile file(styleName);
        file.open(QFile::ReadOnly);
        QString qss = QLatin1String(file.readAll());
        qApp->setStyleSheet(qss);
        qApp->setPalette(QPalette(QColor("#F0F0F0")));
    }

    //窗体居中显示
    static void FormInCenter(QWidget *frm)
    {
        int frmX = frm->width();
        int frmY = frm->height();
        QDesktopWidget w;
        int deskWidth = w.width();
        int deskHeight = w.height();
        QPoint movePoint(deskWidth / 2 - frmX / 2, deskHeight / 2 - frmY / 2);
        frm->move(movePoint);
    }

    //显示信息
    static void ShowMessageTip(QString info, QWidget *parent = nullptr)
    {
        TMessageTip *msg = new TMessageTip(info, parent);
        msg->show();
    }

    //显示信息框,仅确定按钮
    static void ShowMessageBoxInfo(QString info, QWidget *parent = nullptr)
    {
        TMessageBox *msg = new TMessageBox(parent);
        msg->SetMessage(info, 0);
        msg->exec();
    }

    //显示错误框,仅确定按钮
    static void ShowMessageBoxError(QString info, QWidget *parent = nullptr)
    {
        TMessageBox *msg = new TMessageBox(parent);
        msg->SetMessage(info, 2);
        msg->exec();
    }

    //显示询问框,确定和取消按钮
    static bool ShowMessageBoxQuesion(QString info, QWidget *parent = nullptr)
    {
        TMessageBox *msg = new TMessageBox(parent);
        msg->SetMessage(info, 1);
        return msg->exec() == 1;
    }

    //创建数据列表
    static QTableWidget* GetTableWidget(QStringList headers
                                        , int sectionSize = 100
                                        , bool verticalHidden = true
                                        , bool singleSelection = true
                                        , bool rowColors = true)
    {
        QTableWidget *tableWidget = new QTableWidget();
        tableWidget->setRowCount(1);
        tableWidget->setColumnCount(headers.size());
        tableWidget->setHorizontalHeaderLabels(headers);
        tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不可编辑
        tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为时每次选择一行
        if(singleSelection) tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);//只能选择一行 不能多行
        tableWidget->horizontalHeader()->setSectionResizeMode(headers.size()-1, QHeaderView::Stretch);//第一列自动宽度
        tableWidget->horizontalHeader()->setDefaultSectionSize(sectionSize);//默认宽度
        tableWidget->verticalHeader()->setHidden(verticalHidden);
        tableWidget->setAlternatingRowColors(rowColors);
        tableWidget->setFocusPolicy(Qt::NoFocus);//去除选中虚线框和背景 不能通过单机或tab获取焦点

//        tableWidget->setColumnHidden(0, true);//隐藏
//        tableWidget->setColumnWidth(0,100);

        return tableWidget;
    }

    //创建单元格
    static QTableWidgetItem* GetTableWidgetItem(int val, int alignment = Qt::AlignCenter, const QColor &color = nullptr)
    {
        return GetTableWidgetItem(QString::number(val), alignment, color);
    }
    static QTableWidgetItem* GetTableWidgetItem(float val, int alignment = Qt::AlignCenter, const QColor &color = nullptr)
    {
        return GetTableWidgetItem(QString::number(val), alignment, color);
    }
    static QTableWidgetItem* GetTableWidgetItem(double val, int alignment = Qt::AlignCenter, const QColor &color = nullptr)
    {
        return GetTableWidgetItem(QString::number(val), alignment, color);
    }
    static QTableWidgetItem* GetTableWidgetItem(QDateTime datetime, int alignment = Qt::AlignCenter, const QColor &color = nullptr)
    {
        return GetTableWidgetItem(datetime.toString("yyyy-MM-dd HH:mm:ss"), alignment, color);
    }
    static QTableWidgetItem* GetTableWidgetItem(QDate date, int alignment = Qt::AlignCenter, const QColor &color = nullptr)
    {
        return GetTableWidgetItem(date.toString("yyyy-MM-dd"), alignment, color);
    }
    static QTableWidgetItem* GetTableWidgetItem(QTime time, int alignment = Qt::AlignCenter, const QColor &color = nullptr)
    {
        return GetTableWidgetItem(time.toString("HH:mm:ss"), alignment, color);
    }
    static QTableWidgetItem* GetTableWidgetItem(QString text, int alignment = Qt::AlignCenter, const QColor &color = nullptr)
    {
        QTableWidgetItem *item = new QTableWidgetItem(text);
        item->setTextAlignment(alignment);
        if(color != nullptr) item->setTextColor(color);
        return item;
    }

    static QList<int> GetTableSelectedRows(QTableWidget *tableWidget){
        QList<int> rows;
        if(!tableWidget){
            return rows;
        }

        QMap<int, int> rowMap;
        QList<QTableWidgetItem*>items = tableWidget->selectedItems();
        int count = items.count();
        for(int i=0;i<count;i++) {
            int row = tableWidget->row(items.at(i));//获取选中的行
            rowMap.insert(row, row);
        }
        QMap<int, int>::iterator it;
        for (it = rowMap.begin(); it != rowMap.end(); ++it) {
            rows.append(it.key());
        }

        return rows;
    }

    //获得SpinBox
    static QSpinBox* GetSpinBox(int min = 0, int max = 100, QString suffix = "")
    {
        QSpinBox *spinBox = new QSpinBox();
        if(!suffix.isEmpty()) spinBox->setSuffix(" " + suffix);
        spinBox->setRange(min, max);
        spinBox->setAlignment(Qt::AlignRight);
        return spinBox;
    }
    static QSpinBox* GetFreqSpinBox(){
        return GetSpinBox(0, 108000, "kHz");
    }

    //获得数字输入框
    static QLineEdit* GetIntEdit(int min = 0, int max = 100)
    {
        QLineEdit *edit = new QLineEdit();
        edit->setValidator(new QIntValidator(min, max));
        edit->setAlignment(Qt::AlignRight);
        return edit;
    }

    static QLineEdit* GetDoubleEdit(double min = 0.0, double max = 100.0, int decimals = 2)
    {
        QLineEdit *edit = new QLineEdit();
        edit->setValidator(new QDoubleValidator(min, max, decimals));
        edit->setAlignment(Qt::AlignRight);
        return edit;
    }


    //获得波段下拉框
    static QComboBox* GetBandComboBox(bool full = true, bool edit = false){
        QComboBox *combox = new QComboBox();
        combox->setEditable(edit);
        if(full) combox->addItem("--波段--", -1);
        combox->addItem("中波", 0);
        combox->addItem("短波", 1);
        return  combox;
    }

    //获得时间下拉框
    static QComboBox* GetTimeComboBox(bool full = true, bool edit = false){
        QComboBox *combox = new QComboBox();
        if(full) combox->addItem("--时间--", -1);
        combox->setEditable(edit);
        for(int i = 0; i<48; i++){
            int time = i*30;
            int minute = time % 60;
            int hour = time / 60;
            QString text = QString("%1:%2").arg(hour,2,10,QLatin1Char('0')).arg(minute,2,10,QLatin1Char('0'));
            combox->addItem(text, i+1);
        }
        return  combox;
    }


    //添加阴影
    static QGraphicsDropShadowEffect* GetGraphicsDropShadowEffect()
    {
        QGraphicsDropShadowEffect *e = new QGraphicsDropShadowEffect();
        e->setBlurRadius(9.0);
        e->setColor(QColor(0, 0, 0, 160));
        e->setOffset(5.0);
        return e;
    }

    static QGroupBox* GetGroupBox(QWidget *widget, int margin = 5, bool border = false, QString title = ""){
        QString sytle = QString("QGroupBox{%1 %2}")
                .arg(title.isEmpty() ? "margin-top: 0px;" : "")
                .arg(border ? "border: 1px solid #1978D9;" : "");

        QHBoxLayout *layout = new QHBoxLayout();
        layout->setMargin(margin);
        layout->addWidget(widget);

        QGroupBox *groupbox = new QGroupBox();
        groupbox->setStyleSheet(sytle);
        groupbox->setLayout(layout);
        return groupbox;
    }

    static QGroupBox* GetGroupBox(QLayout *layout, bool border = false, QString title = ""){
        QString sytle = QString("QGroupBox{%1 %2}")
                .arg(title.isEmpty() ? "margin-top: 0px;" : "")
                .arg(border ? "border: 1px solid #1978D9;" : "");

        QGroupBox *groupbox = new QGroupBox();
        groupbox->setStyleSheet(sytle);
        groupbox->setLayout(layout);
        return groupbox;
    }

};

#endif // MYGUI_H
